package org.com.tools.backup;

import java.util.HashSet;
import java.util.Set;

import org.com.system.email.Email;
import org.com.system.user.pojo.Resource;
import org.com.system.user.pojo.Role;
import org.com.system.user.pojo.User;
import org.com.system.util.utils.DateUtils;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 * 系统安装第一次运行时的准备数据。
 * 运行此类
 * @author Administrator
 *
 */
public class ReadyData {

	private static SessionFactory sessionFactory=null;
	//private static UserService userServer=null;
	private static ApplicationContext appContext=null;
	
	public static void init(){
		appContext=new ClassPathXmlApplicationContext("springContext.xml");
		//userServer=(UserService)appContext.getBean("userServer");
		sessionFactory=(SessionFactory)appContext.getBean("sessionFactory");
	}
	
	public static void ready(Session session){
		Resource home=new Resource();
		home.setModelName("home");
		home.setModelType("thang");
		home.setOpt("一级模块");
		home.setValue("0");//启用
		
		Set<Resource> homeSub=new HashSet<Resource>();
		
		Resource homeSub_client=new Resource();
		homeSub_client.setModelName("client");
		homeSub_client.setModelType("thang");
		homeSub_client.setOpt("二级模块");
		homeSub_client.setValue("0");
		
		Resource homeSub_data=new Resource();
		homeSub_data.setModelName("data");
		homeSub_data.setModelType("thang");
		homeSub_data.setOpt("二级模块");
		homeSub_data.setValue("0");
		
		homeSub.add(homeSub_client);
		homeSub.add(homeSub_data);
		
		home.setChildren(homeSub);//首页
		
		//==================================================================================
		Resource office=new Resource();
		office.setModelName("office");
		office.setModelType("thang");
		office.setOpt("一级模块");
		office.setValue("0");//启用
		
		Set<Resource> officeSub=new HashSet<Resource>();
		
		Resource officeSub_incept=new Resource();
		officeSub_incept.setModelName("incept");
		officeSub_incept.setModelType("thang");
		officeSub_incept.setOpt("二级模块");
		officeSub_incept.setValue("0");
		
		Resource officeSub_insend=new Resource();
		officeSub_insend.setModelName("insend");
		officeSub_insend.setModelType("thang");
		officeSub_insend.setOpt("二级模块");
		officeSub_insend.setValue("0");
		
		officeSub.add(officeSub_incept);
		officeSub.add(officeSub_insend);
		
		home.setChildren(officeSub);//办公管理
		
		//======================================================================
		 //人事管理
		Resource person=new Resource();
		person.setModelName("person");
		person.setModelType("thang");
		person.setOpt("一级模块");
		
		//==========================================================
		  //信息发布
		Resource info=new Resource();
		info.setModelName("info");
		info.setModelType("thang");
		info.setOpt("一级模块");
		
		
		//==========================================================
		   //档案管理
		Resource archive=new Resource();
		archive.setModelName("archive");
		archive.setModelType("thang");
		archive.setOpt("一级模块");
		
		//==========================================================
		//系统设置
		Resource system=new Resource();
		system.setModelName("system");
		system.setModelType("thang");
		system.setOpt("一级模块");
		
		//=============================================
		  Resource root=new Resource();
		  root.setModelName("administrator");
		  root.setModelType("system");
		  root.setOpt("超级资源");
		  root.setValue("0");
		  
		  Set<Resource> administraor=new HashSet<Resource>();
		  administraor.add(home);
		  administraor.add(office);
		  administraor.add(person);
		  administraor.add(info);
		  administraor.add(archive);
		  administraor.add(system);
		  
		  root.setChildren(administraor);
		  
		
		//Session session=sessionFactory.openSession();
		session.save(root);
	/*	session.save(office);
		session.save(person);
		session.save(info);
		session.save(archive);
		session.save(system);*/
		//session.close();
		
		
		
		//===================================================================
		//创建角色
		
		Role adminRole=new Role();
		adminRole.setRoleName("管理员");
		adminRole.setOpt("系统管理员，拥有所有权限。");
		Set<Resource> ad=new HashSet<Resource>();
		ad.add(root);
		adminRole.setRes(ad);
		
		
		Role userRole=new Role();
        userRole.setOpt("一级用户，除系统设置外，拥有其它任何权限。");		
		userRole.setRoleName("普通用户");
		administraor.remove(system);
		userRole.setRes(administraor);
		
		
		
		//=====================================================================
		//创建用户
		User admin=new User();
		admin.setLoginName("admin");
		admin.setLoginPass("000000");
		admin.setUserName("张勇涛");
		admin.setBirth("1987-09-24");
		
		Set<Role> adminR=new HashSet<Role>();
		adminR.add(adminRole);
		admin.setRoles(adminR);
		
		admin.setCreatePerson("admin");
		admin.setCreateTime(DateUtils.getSystime());
		admin.setOpt("数据由安装人员装载，我是管理");
		admin.setSex(1);
        admin.setEmail(new Email());
		
		
	}
	

	
	public static void main(String[] args) {
		init();
		Session session=sessionFactory.openSession();
		Transaction tx=session.beginTransaction();
		ready(session);
		tx.commit();
		session.close();
	}
	
}
